package org.mspring.mlog.web.module.web.forums;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.bbs.Forum;
import org.mspring.mlog.entity.bbs.Topic;
import org.mspring.mlog.entity.bbs.TopicReply;
import org.mspring.mlog.service.bbs.ForumService;
import org.mspring.mlog.service.bbs.TopicReplyService;
import org.mspring.mlog.service.bbs.TopicService;
import org.mspring.mlog.service.stat.TopicStatService;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.utils.ValidatorUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 主题操作
 * 
 * @author Gao Youbo
 * @since 2013年8月30日
 */
@Widget
@RequestMapping("/forums/topic")
public class BBS_TopicWidget extends BBS_AbstractWidget {

    @Autowired
    private TopicService topicService;
    @Autowired
    private ForumService forumService;
    @Autowired
    private TopicReplyService topicReplyService;
    @Autowired
    private TopicStatService topicStatService;

    /**
     * 帖子页面
     * 
     * @param id
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/{id}")
    public String view(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        Topic topic = topicService.get(id);
        if (topic == null) {
            return "skin:/forums/topic/view";
        }
        model.addAttribute("topic", topic);

        Integer pageSize = 12;
        Integer pageNo = null;

        // 获取回帖总数量
        int totalCount = topicReplyService.getReplyCount(id);

        String page = RequestUtils.getRequestParameterAsString(request, "page");
        if (StringUtils.isNotBlank(page)) {
            if (ValidatorUtils.isNumber(page.trim())) {
                pageNo = Integer.parseInt(page.trim());
            } else if ("last".equalsIgnoreCase(page.trim())) {
                // 获取最后一页的页码
                if (totalCount % pageSize > 0) {
                    pageNo = totalCount / pageSize + 1;
                } else {
                    pageNo = totalCount / pageSize;
                }
            }
        }
        if (pageNo == null) {
            pageNo = 1;
        }

        // 获取该页数据
        List<TopicReply> replys = topicReplyService.listReplyByTopic(id, pageNo, pageSize, null);
        Page<TopicReply> replyPage = new Page<TopicReply>();
        replyPage.setPageSize(pageSize);
        replyPage.setPageNo(pageNo);
        replyPage.setTotalCount(totalCount);
        replyPage.setResult(replys);

        // 设置点击量
        topicStatService.setViews(id);
        // 设置回帖数
        if (topic.getReplys() == null || topic.getReplys() != totalCount) {
            topicStatService.setReplys(id);
        }

        model.addAttribute("replyPage", replyPage);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);

        // 统计数据
        model.addAttribute("views", topicStatService.getViews(id));
        model.addAttribute("replys", totalCount);
        return "skin:/forums/topic/view";
    }

    /**
     * 发表帖子页面
     * 
     * @param topic
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/post")
    public String post(@RequestParam(required = false) Long forum, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Forum> forums = forumService.getAll();
        if (forum != null) {
            model.addAttribute("forum", forum);
        }
        model.addAttribute("forums", forums);
        model.addAttribute(new Topic());
        return "skin:/forums/topic/post";
    }

    /**
     * 发表帖子
     * 
     * @param id
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/post/save")
    public String post_save(@ModelAttribute Topic topic, HttpServletRequest request, HttpServletResponse response, Model model) {
        topic.setAgent(RequestUtils.getUserAgent(request));
        topic.setPostIp(RequestUtils.getRemoteIP(request));
        topic = topicService.createTopic(topic);
        return "redirect:/forums/topic/" + topic.getId();
    }

    /**
     * 回复帖子
     * 
     * @param topicReply
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/reply")
    public String reply(@ModelAttribute TopicReply topicReply, HttpServletRequest request, HttpServletResponse response, Model model) {
        topicReply.setAgent(RequestUtils.getUserAgent(request));
        topicReply.setPostIp(RequestUtils.getRemoteIP(request));
        topicReply = topicReplyService.createReply(topicReply);
        return "redirect:/forums/topic/" + topicReply.getTopic().getId() + "?page=last#reply_" + topicReply.getId();
    }
}
